package com.changhongit.utils;


import java.io.File;
import java.io.IOException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.changhongit.App;
import org.springframework.util.ResourceUtils;


public class TXSDKTokenUtil {
   // private static final String PUBLIC_KEY="src/main/resources/rsa_public_key.pem";
    //private static final String PRIVATE_KEY="src/main/resources/rsa_private_key.pem";

    //String keyPath = IDPRoleTest.class.getResource("").getPath() + "keys/rsa_private_key.pem";


    private static final String HS256_Secret="sLxQ1S2s82envbh4boiTJ4HzMFNEdYWx";



    private static String IDTokenEncode(RSAPublicKey publicKey, RSAPrivateKey privateKey, Map<String,Object> headerClaims,Map<String,Object> payloadClaims){
        try{
            Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
            String token = JWT.create()
                    .withHeader(headerClaims)
                    .withSubject((String) payloadClaims.get("sub"))
                    .withIssuer((String) payloadClaims.get("iss"))
                    .withClaim("name", (String) payloadClaims.get("name"))
                    .withExpiresAt((Date)payloadClaims.get("exp"))
                    .withIssuedAt( (Date)payloadClaims.get("iat"))
                    .sign(algorithm);
            return token;

        }catch (JWTCreationException exception){
            exception.getMessage();
        }
        return null;
    }

    public static String getIdToken() throws IOException {

            File file = null;
            String keyPath1=null;
            String keyPath2=null;
            try {
                file = ResourceUtils.getFile("classpath:rsa_private_key.pem");
                keyPath1 = file.getCanonicalPath();
                System.out.println(keyPath1);
                file = ResourceUtils.getFile("classpath:rsa_public_key.pem");
                keyPath2 = file.getCanonicalPath();
                System.out.println(keyPath2);
            } catch (IOException e) {
                e.printStackTrace();
            }

            long iatIdTokenTimeMills = System.currentTimeMillis();
            long expIdTokenTimeMills = iatIdTokenTimeMills+(long)24*60*60*1000;

            RSAPublicKey publicKey= (RSAPublicKey) PemUtils.readPublicKeyFromFile(keyPath2,"RSA");
            RSAPrivateKey privateKey= (RSAPrivateKey) PemUtils.readPrivateKeyFromFile(keyPath1,"RSA");
            Map<String,Object> idTokenHeaderClaims=new HashMap<String,Object>();
            Map<String,Object> idTokenPayloadClaims=new HashMap<String,Object>();
            idTokenHeaderClaims.put("alg","RS256");
            idTokenHeaderClaims.put("typ","JWT");

            idTokenPayloadClaims.put("sub","sunhongyu");
            idTokenPayloadClaims.put("iss","16600161175");
            idTokenPayloadClaims.put("name","孙宏宇");
            idTokenPayloadClaims.put("iat",new Date(iatIdTokenTimeMills));
            idTokenPayloadClaims.put("exp",new Date(expIdTokenTimeMills));

        String token = IDTokenEncode(publicKey, privateKey, idTokenHeaderClaims, idTokenPayloadClaims);
        return token;

    }

    public static String SDKTokenEncode(String secret,Map<String,Object> headerClaims,Map<String,Object> payloadClaims){
        Algorithm algorithm = Algorithm.HMAC256(secret);
        String token = JWT.create()
                .withHeader(headerClaims)
                .withAudience((String) payloadClaims.get("aud"))
                .withExpiresAt(DateUtil.longToDate((Long) payloadClaims.get("exp")))
                .withIssuedAt(DateUtil.longToDate((Long) payloadClaims.get("iat")))
                .withIssuer((String) payloadClaims.get("iss"))
                .sign(algorithm);
         return token;
    }

    public static String getSDKToken(){
        String sdkSecret=HS256_Secret;
        long iatIdTokenTimeMills = System.currentTimeMillis();
        long expIdTokenTimeMills = iatIdTokenTimeMills+(long)24*60*60*1000;
        Map<String,Object> sdkTokenHeaderClaims=new HashMap<String, Object>();
        Map<String,Object> sdkTokenPayloadClaims=new HashMap<String, Object>();
        sdkTokenHeaderClaims.put("alg","HS256");
        sdkTokenHeaderClaims.put("typ","JWT");
        sdkTokenPayloadClaims.put("aud","Tencent Meeting");
        sdkTokenPayloadClaims.put("iat",iatIdTokenTimeMills);
        sdkTokenPayloadClaims.put("exp",expIdTokenTimeMills);
        sdkTokenPayloadClaims.put("iss","16600161175");

        String token = SDKTokenEncode(sdkSecret, sdkTokenHeaderClaims, sdkTokenPayloadClaims);
        return token;


    }

    public static void main(String[] args) throws IOException {
        String sdkToken = getSDKToken();
         String idToken = getIdToken();

        System.out.println(sdkToken);
       System.out.println(idToken);



    }

}
